#include "StdAfx.h"
#include "SubStructure.h"
#include "jsonreader.h"
#include "Effect.h"
#include "randomseed.h"
#include "EffectFactory.h"

EffectFactory::EffectFactory()
{
	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("etama2.png"), 256, 256, 1, NULL, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
		D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &eff01))
		MessageBox(NULL, _T("etama2.png is not loaded!!!"), _T("ERROR"), MB_OK);
	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("GameSceneText.png"), 256, 256, 1, NULL, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
		D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &eff02))
		MessageBox(NULL, _T("etama2.png is not loaded!!!"), _T("ERROR"), MB_OK);
	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("bomb1.png"), 256, 256, 1, NULL, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
		D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &bomb00))
		MessageBox(NULL, _T("etama2.png is not loaded!!!"), _T("ERROR"), MB_OK);
	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("bomb2.png"), 256, 256, 1, NULL, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
		D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &bomb01))
		MessageBox(NULL, _T("etama2.png is not loaded!!!"), _T("ERROR"), MB_OK);
	
	reader = jsonreader::Instance();
	seed = randomseed::Instance();
}

EffectFactory::~EffectFactory()
{
	if(eff01 != NULL)
	{
		eff01->Release();
	}
	if(eff02 != NULL)
	{
		eff02->Release();
	}
	if(eff03 != NULL)
	{
		eff03->Release();
	}
	if(eff04 != NULL)
	{
		eff04->Release();
	}
	if(bomb00 != NULL)
	{
		bomb00->Release();
	}
	if(bomb01 != NULL)
	{
		bomb01->Release();
	}
}

EffectFactory* EffectFactory::Instance()
{
	if(_p_this == NULL)
	{
		_p_this = new EffectFactory();
	}
	return _p_this;
}

Effect* EffectFactory::EffectMake(Event_struct* Efinfo)
{
	Effect* tEff;
	if(Efinfo ->ID == 43)
	{
		tEff = BombEffectMake(Efinfo);
	}
	else if(Efinfo->ID == 44)
	{
		tEff = CrashEffectMake(Efinfo);
	}
	else if(Efinfo->ID == 45)
	{
		tEff = BulletEffectMake(Efinfo);
	}
	else
	{
	}
	return tEff;
}

Effect* EffectFactory::BombEffectMake(Event_struct* Efinfo)
{
	Effect* tEff = new Effect();
	
	tEff->SetPos(Efinfo->makePos);
	if(Efinfo->kind == 0)
	{
		RECT temp;
		SetRect(&temp, 0, 0, 32, 256); 
		tEff->SetAni(reader->getAni(11, bomb00, temp));
		tEff->SetLifeTime(40);
		tEff->SetMove(reader->getMove(1));
		tEff->SetNowAniState(0);
		tEff->SetNowMoveState(0);
		tEff->SetRadian(M_PI_2);
		tEff->SetRadianVel(0.0f);
		tEff->SetScale(D3DXVECTOR2(1.0f, 1.7f));
		tEff->SetScaleVel(D3DXVECTOR2(0.0f, 0.0f));
		tEff->SetScalelim(D3DXVECTOR2(2.0f, 2.0f));
	}
	else
	{
		RECT temp;
		SetRect(&temp, 0, 0, 256, 128); 
		tEff->SetAni(reader->getAni(12, bomb01, temp));
		tEff->SetLifeTime(58);
		tEff->SetMove(reader->getMove(1));
		tEff->SetNowAniState(0);
		tEff->SetNowMoveState(0);
		tEff->SetRadian(0.0f);
		tEff->SetRadianVel(0.0f);
		tEff->SetScale(D3DXVECTOR2(1.0f, 1.0f));
		tEff->SetScaleVel(D3DXVECTOR2(.0f, .0f));
		tEff->SetScalelim(D3DXVECTOR2(2.0f, 2.0f));
	}
	return tEff;
}
Effect* EffectFactory::CrashEffectMake(Event_struct* Efinfo)
{
	Effect* tEff = new Effect();
	RECT temp;
	if(Efinfo ->kind == 0)
	{
		SetRect(&temp, 192, 176, 256, 240);
		tEff->SetPos(Efinfo->makePos);
		tEff->SetAni(reader->getAni(20, eff01, temp));
		tEff->SetMove(reader->getMove(1));
		tEff->SetScale(D3DXVECTOR2(0.1f, 0.1f));
		tEff->SetScalelim(D3DXVECTOR2(1.0f, 1.0f));
		tEff->SetScaleVel(D3DXVECTOR2(0.045f, 0.045f));
		tEff->SetLifeTime(20);
		tEff->SetNowAniState(0);
		tEff->SetNowMoveState(0);
		tEff->SetRadian(0.0f);
		tEff->SetRadianVel(0.0f);
	}
	else if(Efinfo->kind == 1)
	{
		SetRect(&temp, 0, 176, 64, 240);
		tEff->SetPos(Efinfo->makePos);
		tEff->SetAni(reader->getAni(21, eff01, temp));
		tEff->SetMove(reader->getMove(1));
		tEff->SetScale(D3DXVECTOR2(0.8f, 0.8f));
		tEff->SetScalelim(D3DXVECTOR2(1.4f, 1.4f));
		tEff->SetScaleVel(D3DXVECTOR2(-0.05f, -0.05f));
		tEff->SetLifeTime(40);
		tEff->SetNowAniState(0);
		tEff->SetNowMoveState(0);
		tEff->SetRadian(0.0f);
		tEff->SetRadianVel(0.0f);
	}
	return tEff;
}
Effect* EffectFactory::BulletEffectMake(Event_struct* Efinfo)
{
	Effect* tEff = new Effect();
	RECT temp;
	SetRect(&temp, 64, 208, 96, 240);
	tEff->SetPos(Efinfo->makePos);
	tEff->SetAni(reader->getAni(13, eff01, temp));
	tEff->SetMove(reader->getMove(1));
	tEff->SetScale(D3DXVECTOR2(0.3f, 0.3f));
	tEff->SetScalelim(D3DXVECTOR2(1.0f, 1.0f));
	tEff->SetScaleVel(D3DXVECTOR2(0.05f, 0.05f));
	tEff->SetLifeTime(12);
	tEff->SetNowAniState(0);
	tEff->SetNowMoveState(0);
	tEff->SetRadian(0.0f);
	tEff->SetRadianVel(0.0f);
	return tEff;
}